<?php

namespace app\index\controller;

use app\common\model\OrderList;
use think\Controller;
use think\Db;
use think\Request;
use app\common\model\Orders as OrdersModel;
use houdunwang\cart\Cart;
require_once ROOT_PATH . 'public/wxPaySdk/lib/WxPay.Api.php';
require_once ROOT_PATH.  'public/wxPaySdk/example/WxPay.JsApiPay.php';
/**
 * 订单管理页面
 * Class Orders
 * @package app\index\controller
 */
class Orders extends Controller
{
    //
    public function __construct()
    {
        parent::__construct();
//①、获取用户openid
        $tools = new \JsApiPay();

        if(!session('openId')){
            $openId = $tools->GetOpenid();
            session('openId',$openId);
        }else{
            $openId = session('openId');
        }
//②、统一下单
        $field = session('orders');
//        p($field);die;

//        foreach($field['goods'] as $k=>$v){
//
//        }
        $input = new \WxPayUnifiedOrder();
        $input->SetBody('name');
//$input->SetAttach("test");//附加数据 不是必须
        $input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
//        $input->SetTotal_fee($field['total_price']);
        $input->SetTotal_fee('1');//以分为单位
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
//$input->SetGoods_tag("test");
        $input->SetNotify_url("http://ccshop.weian-weijue.com/index/orders/notify.html");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//printf_info($order);
        $jsApiParameters = $tools->GetJsApiParameters($order);
//var_dump($jsApiParameters);die;
        $this->assign('jsApiParameters',$jsApiParameters);
    }
    public function notify(){
        //修改订单状态
        db('user')->insert(['uname'=>111]);
    }
    public function index()
    {
        //首页数据
        $field = session('orders');
        //送货地址数据
        $uid = 'uid';
        $us = session('user.uname');
        $map = $uid == $us;
        $addressData = Db::table('shop_address')->where($map)->select();
//        p($addressData);die;
        return view('',compact('field','addressData'));
    }

    /**
     * 购物车去结算提交的订单数据
     */
    public function addOrders()
    {
        if(request()->isAjax()){
            //接受post数据
            $sid = input('post.sid');
            //halt($sid);//string(99) "dc2d88ad4d47b16e33eec381d9483fa3|71e3dd8a288cd30c59169b405f5071c7|57f1afc5bab50bc28d67391036a6da68|"
            $sid = explode('|',rtrim($sid,'|'));//拆分数组
//            p($sid);die;
            $orderData = [];//接收的订单数组
            $goods = session('cart.goods');
//            p($goods);die;
            foreach($sid as $k=>$v){
                $orderData['goods'][] = $goods[$v];
            }
//            p($orderData);
            //计算商品总条数和总价
            $orderData['total_row'] = 0;
            $orderData['total_price'] = 0;
            foreach($orderData['goods'] as $k=>$v){
                $orderData['total_row'] += $v['num'];
                $orderData['total_price'] += $v['total'];
            }
            //halt($orderData);
            //将数据存入到session中
            session('orders',$orderData);
            echo 1;die;
        }
    }

    /**
     *  添加用户收货人信息 数据表order
     *  生成订单 并创建订单列表
     */
    public function buyNow()
    {
        //获取session用户下单商品数据
        $ordersData = session('orders');
//        halt($ordersData); die;
        //添加订单表和订单列表
        $orders = (new OrdersModel());// { 获取数据表将数据按需求今天填充 }
//        halt($orders);
        $orders['time'] = time();//时间
        $orders['number'] = Cart::getOrderId();//订单号
        $orders['total_price'] = $ordersData['total_price'];//总价
        $orders['total_num'] = $ordersData['total_row'];//购买数量
        $orders['status'] = 1;//下单状态
        $orders['uid'] = session('user.uid');//用户id
        $orders['consignee'] = session('user.uname');//用户，发货人
        $orders['address'] = input('post.id');

        $orders->save();
//        halt($orders);
        //添加订单列表页
        foreach($ordersData['goods'] as $k=>$v){
            $orderList = (new OrderList());
            $orderList['num'] = $v['num'];//购买数量
            $orderList['name'] = $v['name'];//商品名称
            $orderList['gid'] = $v['id'];//商品id
            $orderList['pic'] = $v['options']['img'];//商品展示图
            $orderList['total'] = $v['total'];//小计
            $orderList['spec'] = $v['options']['color'] . '|' . $v['options']['size'];//商品规格
            $orderList['orders_id'] = $v['id'];//订单id
            $orderList['consignee'] = session('user.uname');//用户，发货人
            $orderList->save();
        }
        echo 1;
    }

}
